#!/bin/bash

export 'PS4=[`date +"%Y-%m-%d %H:%M:%S"`]`basename $0`.$LINENO+ '
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

SRC_DIR=$1

if [ ! -d $1 ]; then
    mkdir -p $1
fi

check_style()
{
    cd $GERRIT_PROJECT
    git log -n 1 | grep -w "LTS" && {  return 0; }
    rm -f ${WORKSPACE}/patchlist
    if git diff origin/master --name-status HEAD~1 HEAD~0 | egrep '*\.patch$' &> /dev/null && git diff origin/master --name-status HEAD~1 HEAD~0 | egrep '*\.c$|*\.h$' &> /dev/null; then
        echo "Error: patch and c/h can't commit together"
        exit 1
    elif git diff origin/master --name-status HEAD~1 HEAD~0 | egrep '*\.patch$' &> /dev/null; then
        git diff origin/master --name-status HEAD~1 HEAD~0 | egrep '*\.patch$' | egrep -v '^D' | awk '{print $2}' >> ${WORKSPACE}/patchlist
    else
        git format-patch -U4 -n1 --no-binary >> ${WORKSPACE}/patchlist
    fi
    failed_patch_num=0
    failed_patch=''
    for patchname in `cat ${WORKSPACE}/patchlist`
    do
        #remove_spec $patchname  #Does not appear to be a unified-diff format patch
        sed -i "/^Change-Id: /d" $patchname
        rm -f $WORKSPACE/checkpatch.log

        rm -f $Kernel_Src_Tree/spelling.txt;touch $Kernel_Src_Tree/spelling.txt
        cp -a ${SCRIPT_DIR}/checkpatch.pl $Kernel_Src_Tree && chmod 777 $Kernel_Src_Tree/checkpatch.pl
        $Kernel_Src_Tree/checkpatch.pl --terse --no-tree --no-signoff $patchname | tee -a $WORKSPACE/checkpatch.log

        #没有风格问题，checkpatch.log为空
        if [ ! -s $WORKSPACE/checkpatch.log ]; then
            continue
        fi
        if ! tail -n 3 $WORKSPACE/checkpatch.log | grep "total: 0 errors," &> /dev/null;then
            echo ""
            echo "==================PATCH($patchname) content start================="
            cat -n $patchname
            echo "==================PATCH($patchname) content end================="
            echo ""
            echo ""
            echo "======================ERROR:coding style error======================"
            echo "======================check patch upper output which start with PATCH($patchname) content start======================"
            while read line
            do
                echo -e "\033[31m"$line" \033[0m"
            done < $WORKSPACE/checkpatch.log
            echo ""
            echo ""
            
            rm -f $WORKSPACE/checkpatch.log
            ((failed_patch_num = failed_patch_num + 1))
            failed_patch="$failed_patch $patchname"
        fi
    done
	for patchname in `cat ${WORKSPACE}/patchlist`
    do
		rm -f $patchname
	done
    if [[ ${failed_patch_num} -ge 1 ]]; then
        echo "${failed_patch_num} patch has failed:$failed_patch"
        exit 0
    fi
}

remove_spec()
{
    patch=$1

    start_no=0
    stop_no=0
    diff_list=$WORKSPACE/diff_list
    cat $patch | grep -n "diff --git" > $diff_list
    #read in reverse order: Reduced row count related computation
    tac $diff_list | while read line
    do
        line_no=`echo "$line" | awk -F ':' '{print $1}'`
        if echo "$line" | grep -E "*\.spec" &> /dev/null;then
            if [ $stop_no -eq 0 ];then
                #the change is at the end of the patch, so delete the parts of content after the start_no
                start_no=$line_no
                sed -i "${start_no},$ d" $patch
                start_no=0
                continue
            else
                #has found a stop_no, here is the start_no to it
                start_no=$line_no
            fi
        else
             stop_no=$line_no
        fi
        if [ $start_no -ne 0 -a $stop_no -ne 0 -a $stop_no -gt $start_no ];then
            #the stop_no belongs to the other file,so excludes it
            let "stop_no = $stop_no - 1"
            sed -i "${start_no},${stop_no} d" $patch
            #the parts before the stop_no has deleted, so change it to start_no as new stop_no
            stop_no=$start_no
        fi
    done
    cat $patch | grep -E "*\.spec" | grep -E "^ " > $diff_list
    cat $diff_list | while read line
    do
        line_del=`cat $patch | grep -n "$line" | awk -F ':' '{print $1}'`
        sed -i "$line_del d" $patch
        insert_no=`echo "$line" | awk '{print $4}' | awk -F '-' '{print $1}' | wc -c`
        let "insert_no = $insert_no - 1"
        delete_no=`echo "$line" | awk '{print $4}' | awk -F '-' '{print $2}' | wc -c`

        files=`cat $patch | grep "files changed," | awk '{print $1}'`
        insertions=`cat $patch | grep "files changed," | awk '{print $4}'`
        deletion=`cat $patch | grep "files changed," | awk '{print $6}'`
        set +e
        let "files = $files - 1"
        let "insertions = $insertions - $insert_no"
        let "deletion = $deletion - $delete_no"
        set -e

        line_insert=`cat $patch | grep -n "files changed," | awk -F ':' '{print $1}'`
        sed -i "/files changed,/a\ ${files} files changed, ${insertions} insertions(+), ${deletion} deletion(-)" $patch
        sed -i "$line_insert d" $patch
    done
    rm -f $diff_list
}

main()
{
    cd $SRC_DIR
    cd ../linux-4.19   
    GERRIT_PROJECT=$(pwd)    
    [[ "$GERRIT_BRANCH" == "RTOS-TD" ]] && exit 0
    [[ "$GERRIT_BRANCH" == "RTOS-6186" ]] && exit 0
    [[ "$GERRIT_PROJECT" =~ "test_Debug" ]] && exit 0
    Kernel_Src_Tree=$GERRIT_PROJECT
    #checkpatch.pl必须放到内核代码下，才能正常执行
    check_style
}

main
